package com.zhsm.fuxi.mvp.presenter

import com.hankkin.library.mvp.presenter.RxLifePresenter
import com.hankkin.reading.control.UserControl
import com.hankkin.reading.http.HttpClientUtils
import com.zhsm.paipaiout.baselibrary.utils.LoadingUtils
import com.zhsm.paipaiout.baselibrary.utils.ToastUtils
import com.zhsm.fuxi.app.Constant
import com.zhsm.fuxi.mvp.contract.AttContract
import com.zhsm.fuxi.utils.QiniuUpLoadHelper
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

/**
 * Created by GuZhC on 2018/10/9.
 * describe:
 */
class AttPresenter : RxLifePresenter<AttContract.IView>(), AttContract.IPresenter {

    val mQiniuUpLoadHelper: QiniuUpLoadHelper by lazy {
        QiniuUpLoadHelper(Constant.QiNiu.SECRET)
    }

    override fun getAttInfo() {
        HttpClientUtils.Builder.getPaipiaHttp().getAttInfo(UserControl.getId())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeEx({
                    getMvpView().setAttInfo(it)
                }, {
                   LoadingUtils.hideLoading()
                }).bindRxLifeEx(RxLife.ON_DESTROY)

    }


    override fun upPicToQiniu(picPath: String) {
         mQiniuUpLoadHelper.filePath =picPath
        mQiniuUpLoadHelper!!.setUpLoadPicListener(object : QiniuUpLoadHelper.UpLoadPicListener {
            override fun onSuccess(key: String) {
                getMvpView().upQiniuSucess(key)
            }
            override fun onError(msg: String) {
                ToastUtils.makeText(msg)
                getMvpView().hideLoding()
            }
        })
        mQiniuUpLoadHelper!!.uploadPic()
    }

    override fun upAttInfo(map :HashMap<String, Any>) {
        HttpClientUtils.Builder.getPaipiaHttp().upAttInfo(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeEx({
                    getMvpView().upAttInfoSucess(it)
                }, {
                    getMvpView().hideLoding()
                }).bindRxLifeEx(RxLife.ON_DESTROY)

    }

    override fun onDestroy() {
        super.onDestroy()
        mQiniuUpLoadHelper?.let {
            it.setCancle(true)
            it.setUpLoadPicListener(null)
        }
    }
}